package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.domain.CheckHeatFeeExpressionParam;
import com.ruoyi.system.service.ISysDictTypeService;
import org.nutz.el.El;
import org.nutz.lang.Lang;
import org.nutz.lang.util.Context;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysHeatFeeTemplate;
import com.ruoyi.system.service.ISysHeatFeeTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 热费计算模板Controller
 * 
 * @author ruoyi
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/system/template")
public class SysHeatFeeTemplateController extends BaseController
{
    @Autowired
    private ISysHeatFeeTemplateService sysHeatFeeTemplateService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;
    /**
     * 查询热费计算模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysHeatFeeTemplate sysHeatFeeTemplate)
    {
        startPage();
        sysHeatFeeTemplate.setDeptId(getDeptId());
        List<SysHeatFeeTemplate> list = sysHeatFeeTemplateService.selectSysHeatFeeTemplateList(sysHeatFeeTemplate);
        return getDataTable(list);
    }

    /**
     * 导出热费计算模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:export')")
    @Log(title = "热费计算模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysHeatFeeTemplate sysHeatFeeTemplate)
    {
        sysHeatFeeTemplate.setDeptId(getDeptId());
        List<SysHeatFeeTemplate> list = sysHeatFeeTemplateService.selectSysHeatFeeTemplateList(sysHeatFeeTemplate);
        ExcelUtil<SysHeatFeeTemplate> util = new ExcelUtil<SysHeatFeeTemplate>(SysHeatFeeTemplate.class);
        util.exportExcel(response, list, "热费计算模板数据");
    }


    /**
     * 获取热费计算模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId)
    {
        return success(sysHeatFeeTemplateService.selectSysHeatFeeTemplateByTemplateId(templateId));
    }

    /**
     * 新增热费计算模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:add')")
    @Log(title = "热费计算模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysHeatFeeTemplate sysHeatFeeTemplate)
    {
        sysHeatFeeTemplate.setDeptId(getDeptId());
        return toAjax(sysHeatFeeTemplateService.insertSysHeatFeeTemplate(sysHeatFeeTemplate));
    }

    /**
     * 校验表达式
     * @param expression
     * @return
     */
    @PostMapping("/check/expression")
    public AjaxResult check(@RequestBody CheckHeatFeeExpressionParam expression) {
        Context context = Lang.context();
        List<SysDictData> fields = sysDictTypeService.selectDictDataByType("calculate_heat_target");
        for (SysDictData field : fields) {
            context.set(field.getDictValue(), 100);
        }
        return success(El.eval(context, expression.getExpression()));
    }
    /**
     * 修改热费计算模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:edit')")
    @Log(title = "热费计算模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysHeatFeeTemplate sysHeatFeeTemplate)
    {
        return toAjax(sysHeatFeeTemplateService.updateSysHeatFeeTemplate(sysHeatFeeTemplate));
    }

    /**
     * 删除热费计算模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "热费计算模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{templateId}")
    public AjaxResult remove(@PathVariable Long templateId)
    {
        return toAjax(sysHeatFeeTemplateService.deleteSysHeatFeeTemplateByTemplateId(templateId));
    }
}
